/**
 * opportunity控制器
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.controller;


import com.sxf.crm.dto.OpportunityDTO;
import com.sxf.crm.entity.Opportunity;
import com.sxf.crm.service.OpportunityFollowUpService;
import com.sxf.crm.service.OpportunityService;
import com.sxf.crm.util.SecurityUtils;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.sxf.crm.dto.OpportunityFollowUpDTO;
import com.sxf.crm.dto.OpportunityFollowUpRequest;
import com.sxf.crm.dto.OpportunityOperateRequest;

import java.util.List;
import com.sxf.crm.dto.OpportUnityAssignRequest;

@Slf4j
@RestController
@RequestMapping("/api/opportunities")
@RequiredArgsConstructor
public class OpportunityController {

    private final OpportunityService opportunityService;
    private final OpportunityFollowUpService opportunityFollowUpService;
    
    @PostMapping
    @PreAuthorize("hasAuthority('opportunity:create')")
    public ResponseEntity<Opportunity> create(@RequestBody Opportunity opportunity) {
        log.info("接收到创建商机请求: {}", opportunity);
        Long currentUserId = SecurityUtils.getCurrentUserId();
        String currentUsername = SecurityUtils.getCurrentUsername();
        log.debug("创建商机: 当前用户ID={}, 用户名={}, 商机={}", currentUserId, currentUsername, opportunity);
        Opportunity created = opportunityService.create(opportunity, currentUserId);
        return ResponseEntity.ok(created);
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('opportunity:update')")
    public ResponseEntity<Opportunity> update(@PathVariable Long id, @RequestBody Opportunity opportunity) {
        log.info("接收到更新商机请求: id={}, opportunity={}", id, opportunity);
        Long currentUserId = SecurityUtils.getCurrentUserId();
        String currentUsername = SecurityUtils.getCurrentUsername();
        log.debug("更新商机: 当前用户ID={}, 用户名={}, 商机ID={}, opportunity={}", currentUserId, currentUsername, id, opportunity);
        opportunity.setId(id);
        Opportunity updated = opportunityService.update(opportunity, currentUserId);
        return ResponseEntity.ok(updated);
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('opportunity:view')")
    public ResponseEntity<OpportunityDTO> getById(@PathVariable Long id) {
        log.info("接收到获取商机详情请求: id={}", id);
        Opportunity opportunity = opportunityService.getById(id);
        return ResponseEntity.ok(opportunityService.convertToDTO(opportunity));
    }

    @GetMapping
    @PreAuthorize("hasAuthority('opportunity:list')")
    public ResponseEntity<Page<OpportunityDTO>> findAll(Pageable pageable) {
        log.info("接收到分页查询商机列表请求: pageable={}", pageable);
        Page<Opportunity> opportunities = opportunityService.findAll(pageable);
        Page<OpportunityDTO> dtos = opportunities.map(data->opportunityService.convertToDTO(data));
        return ResponseEntity.ok(dtos);
    }

    @PostMapping("/{id}/claim")
    @PreAuthorize("hasAuthority('opportunity:claim')")
    public ResponseEntity<OpportunityDTO> claim(@PathVariable Long id, @RequestBody OpportunityOperateRequest request) {
        log.info("接收到认领商机请求: id={}, remark={}", id, request.getRemark());
        Long currentUserId = SecurityUtils.getCurrentUserId();
        String currentUsername = SecurityUtils.getCurrentUsername();
        log.debug("认领商机: 当前用户ID={}, 用户名={}, 商机ID={}, remark={}", currentUserId, currentUsername, id, request.getRemark());
        Opportunity opportunity = opportunityService.claim(id, request.getRemark(), currentUserId);
        return ResponseEntity.ok(opportunityService.convertToDTO(opportunity));
    }

    @PostMapping("/{id}/assign")
    @PreAuthorize("hasAuthority('opportunity:assign')")
    public ResponseEntity<OpportunityDTO> assign(@PathVariable Long id, @RequestBody OpportUnityAssignRequest request) {
        log.info("接收到分配商机请求: id={}, remark={}", id, request.getRemark());
        Long currentUserId = SecurityUtils.getCurrentUserId();
        String currentUsername = SecurityUtils.getCurrentUsername();
        log.debug("分配商机: 当前用户ID={}, 用户名={}, 商机ID={}, remark={}", currentUserId, currentUsername, id, request.getRemark());
        return ResponseEntity.ok(opportunityService.convertToDTO(opportunityService.assign(id, request.getOwnerId(),request.getRemark(),currentUserId)));
    }

    @PostMapping("/{id}/accept")
    @PreAuthorize("hasAuthority('opportunity:accept')")
    public ResponseEntity<OpportunityDTO> accept(@PathVariable Long id) {
        log.info("接收到接受商机请求: id={}", id);
        Long currentUserId = SecurityUtils.getCurrentUserId();
        String currentUsername = SecurityUtils.getCurrentUsername();
        log.debug("接受商机: 当前用户ID={}, 用户名={}, 商机ID={}", currentUserId, currentUsername, id);
        return ResponseEntity.ok(opportunityService.convertToDTO(opportunityService.accept(id,currentUserId)));
    }

    @PostMapping("/{id}/reject")
    @PreAuthorize("hasAuthority('opportunity:reject')")
    public ResponseEntity<OpportunityDTO> reject(@PathVariable Long id, @RequestBody OpportunityOperateRequest request) {
        log.info("接收到拒绝商机请求: id={}", id);
        Long currentUserId = SecurityUtils.getCurrentUserId();
        String currentUsername = SecurityUtils.getCurrentUsername();
        log.debug("拒绝商机: 当前用户ID={}, 用户名={}, 商机ID={}", currentUserId, currentUsername, id);
        return ResponseEntity.ok(opportunityService.convertToDTO(opportunityService.reject(id,request.getRemark(),currentUserId)));
    }

    @PostMapping("/{id}/recycle")
    @PreAuthorize("hasAuthority('opportunity:recycle')")
    public ResponseEntity<Opportunity> recycle(@PathVariable Long id, @RequestBody OpportunityOperateRequest request) {
        log.info("接收到回收商机请求: id={}", id);
        Long currentUserId = SecurityUtils.getCurrentUserId();
        String currentUsername = SecurityUtils.getCurrentUsername();
        log.debug("回收商机: 当前用户ID={}, 用户名={}, 商机ID={}, request={}", currentUserId, currentUsername, id, request);
        return ResponseEntity.ok(opportunityService.recycle(id, request.getRemark(), currentUserId));
    }

    @PostMapping("/{id}/lost")
    @PreAuthorize("hasAuthority('opportunity:lost')")
    public ResponseEntity<Opportunity> lost(@PathVariable Long id, @RequestBody OpportunityOperateRequest request) {
        log.info("接收到商机丢失请求: id={}, remark={}", id, request.getRemark());
        Long currentUserId = SecurityUtils.getCurrentUserId();
        String currentUsername = SecurityUtils.getCurrentUsername();
        log.debug("商机丢失: 当前用户ID={}, 用户名={}, 商机ID={}, request={}", currentUserId, currentUsername, id, request);   
        return ResponseEntity.ok(opportunityService.lost(id, request.getRemark(), currentUserId));
    }

    @PostMapping("/{id}/won")
    @PreAuthorize("hasAuthority('opportunity:won')")
        public ResponseEntity<Opportunity> won(@PathVariable Long id, @RequestBody OpportunityOperateRequest request) {
        log.info("接收到商机成交请求: id={}", id);
        Long currentUserId = SecurityUtils.getCurrentUserId();
        String currentUsername = SecurityUtils.getCurrentUsername();
        log.debug("商机成交: 当前用户ID={}, 用户名={}, 商机ID={}, request={}", currentUserId, currentUsername, id, request);
        return ResponseEntity.ok(opportunityService.won(id, request.getRemark(), currentUserId));
    }

    @PostMapping("/{id}/negotiating")
    @PreAuthorize("hasAuthority('opportunity:negotiating')")
    public ResponseEntity<Opportunity> negotiating(@PathVariable Long id, @RequestBody OpportunityOperateRequest request) {
        log.info("接收到商机谈判中请求: id={}", id);
        Long currentUserId = SecurityUtils.getCurrentUserId();
        String currentUsername = SecurityUtils.getCurrentUsername();
        log.debug("商机谈判中: 当前用户ID={}, 用户名={}, 商机ID={}, request={}", currentUserId, currentUsername, id, request);
        return ResponseEntity.ok(opportunityService.negotiating(id, request.getRemark(), currentUserId));
    }

    @PostMapping("/{id}/pending-decision")
    @PreAuthorize("hasAuthority('opportunity:pending-decision')")
    public ResponseEntity<Opportunity> pendingDecision(@PathVariable Long id, @RequestBody OpportunityOperateRequest request) {
        log.info("接收到商机待决策请求: id={}", id);
        Long currentUserId = SecurityUtils.getCurrentUserId();
        String currentUsername = SecurityUtils.getCurrentUsername();
        log.debug("商机待决策: 当前用户ID={}, 用户名={}, 商机ID={}, request={}", currentUserId, currentUsername, id, request);
        return ResponseEntity.ok(opportunityService.pendingDecision(id, request.getRemark(), currentUserId));
    }

    @PostMapping("/{id}/in-progress")
    @PreAuthorize("hasAuthority('opportunity:in-progress')")
    public ResponseEntity<Opportunity> inProgress(@PathVariable Long id, @RequestBody OpportunityOperateRequest request) {
        log.info("接收到商机进行中请求: id={}", id);
        Long currentUserId = SecurityUtils.getCurrentUserId();
        String currentUsername = SecurityUtils.getCurrentUsername();
        log.debug("商机进行中: 当前用户ID={}, 用户名={}, 商机ID={}, request={}", currentUserId, currentUsername, id, request);
        return ResponseEntity.ok(opportunityService.inProgress(id, request.getRemark(), currentUserId));
    }

    @PostMapping("/{id}/followed-up")
    @PreAuthorize("hasAuthority('opportunity:followed-up')")
    public ResponseEntity<Opportunity> followedUp(@PathVariable Long id, @RequestBody OpportunityOperateRequest request) {
        log.info("接收到商机跟进中请求: id={}", id);
        Long currentUserId = SecurityUtils.getCurrentUserId();
        String currentUsername = SecurityUtils.getCurrentUsername();
        log.debug("商机跟进中: 当前用户ID={}, 用户名={}, 商机ID={}, request={}", currentUserId, currentUsername, id, request);
        return ResponseEntity.ok(opportunityService.followedUp(id, request.getRemark(), currentUserId));
    }

    @GetMapping("/{id}/followups")
    @PreAuthorize("hasAuthority('opportunity:followup')")
    public ResponseEntity<List<OpportunityFollowUpDTO>> listFollowUps(@PathVariable Long id) {
        log.debug("获取线索跟进记录: clueId={}", id);
        return ResponseEntity.ok(opportunityFollowUpService.listByOpportunityId(id));
    }
    
    @PostMapping("/{id}/followups")
    @PreAuthorize("hasAuthority('opportunity:followup')")
    public ResponseEntity<OpportunityFollowUpDTO> addFollowUp(@PathVariable Long id, @RequestBody OpportunityFollowUpRequest request) {
        log.debug("添加线索跟进: clueId={}, request={}", id, request);
        Long currentUserId = SecurityUtils.getCurrentUserId();
        String currentUsername = SecurityUtils.getCurrentUsername();
        log.debug("添加线索跟进: 当前用户ID={}, 用户名={}, clueId={}, request={}", currentUserId, currentUsername, id, request);
        return ResponseEntity.ok(opportunityFollowUpService.addFollowUp(id, request,currentUserId));
    }
    
}